//
//  FoldHeaderView.m
//  day-02-TopView
//
//  Created by hcios on 2017/6/30.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "FoldHeaderView.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width

#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@implementation FoldHeaderView{
    
    BOOL _created;  //是否创建过
    UILabel *_titleLabel;   //标题
    UILabel *_detailLabel;  //其他内容
    UIImageView *_imageView;    //图标
    UIButton *_btn; //收起按钮
    BOOL _canFold;  //是否可扩展
}


-(void)setWeatherSectionHeaderViewWithTitle:(NSString *)title detail:(NSString *)detail type:(HeaderStyle)type section:(NSInteger)section canFold:(BOOL)canFold{
    
    if (!_created) {
        [self creatUI];
    }
    _titleLabel.text = title;
    if (type == HeaderStyleNone) {
        _detailLabel.hidden = YES;
    }else{
        _detailLabel.hidden = NO;
        _detailLabel.attributedText = [self attributeStringWith:detail];
    }
    _section = section;
    _canFold = canFold;
    if (canFold) {
        _imageView.hidden = NO;
    }else{
        _imageView.hidden = YES;
    }
}


-(NSMutableAttributedString *)attributeStringWith:(NSString *)string{
    NSString *str = [NSString stringWithFormat:@"展开：%@",string];
    NSMutableAttributedString *ats = [[NSMutableAttributedString alloc] initWithString:str];
    NSRange range = [str rangeOfString:string];
    [ats setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} range:range];
    return ats;
}


-(void)creatUI{
    _created = YES;
    
    //标题
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 90, 30)];
    _titleLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_titleLabel];
    
    //按钮
    _btn = [UIButton buttonWithType:UIButtonTypeCustom];
    _btn.frame = CGRectMake(0, 0, kScreenWidth, 30);
    [_btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_btn];
    
    //图片
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth - 30, 15, 8, 9)];
    _imageView.image = [UIImage imageNamed:@"arrow_down_gray"];
    [self.contentView addSubview:_imageView];
    
    //线
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 39, kScreenWidth, 1)];
    line.image = [UIImage imageNamed:@"line"];
    [self.contentView addSubview:line];
    
    
}

-(void)setFold:(BOOL)fold{
    _fold = fold;
    if (fold) {
        _imageView.image = [UIImage imageNamed:@"arrow_down_gray"];
    }else{
        _imageView.image = [UIImage imageNamed:@"arrow_up_gray"];
    }
}


#pragma mark - 按钮点击事件
-(void)btnClick:(UIButton *)btn{
    if (_canFold) {
        if ([self.delegate respondsToSelector:@selector(foldHeaderInSection:)]) {
            [self.delegate foldHeaderInSection:_section];
        }
    }
}

@end
